package com.example.bgmachine.bgmachine.popinputs;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.handlouts.HandlContentData;
import com.example.bgmachine.bgmachine.homes.HomePages;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

public class PopinputAdapter extends BaseAdapter {
    private Context mContext;
    private LinkedList<PopInputData> mData = new LinkedList<PopInputData>();

    public PopinputAdapter(Context context, LinkedList<PopInputData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        PopinputAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.pop_input_list_item_content, null);
            convertViewtms= convertView;
            holder = new ViewHolder();

            holder.mHandlContentData = mData.get(position);


            holder.frament_handl_menu_textview   = convertView.findViewById(R.id.frament_handl_menu_textview);
            holder.frament_handl_index_textview   = convertView.findViewById(R.id.frament_handl_index_textview);

            holder.frament_handl_input_edittext   = convertView.findViewById(R.id.frament_handl_input_edittext);


            holder.frament_handl_menu_textview.setText( holder.mHandlContentData.getMenuName());
            holder.frament_handl_input_edittext.setText(  holder.mHandlContentData.getMenuOutData() );
            holder.frament_handl_index_textview.setText(  holder.mHandlContentData.getMenuid()+"");


            holder.frament_handl_input_edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_NEXT) {
                        PopinputAdapter.ViewHolder holdera = (PopinputAdapter.ViewHolder) v.getTag();
                        Log.d("EditText", "输入完成="+v.getText());
                        holdera.mHandlContentData.setMenuOutData(v.getText().toString());
                        Log.d("EditText", "输入完成=1="+holdera.mHandlContentData.getMenuOutData() );
                        JSONObject menuOutJson = holdera.mHandlContentData.getMenuOutJson();
                        if(menuOutJson != null){
                            try {
                                menuOutJson.put("setbeverageOutput",v.getText().toString() );
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        return false; // 消费事件，避免默认行为
                    }
                    return false;
                }
            });
            holder.frament_handl_input_edittext.setTag(holder);



            convertView.setTag(holder);
        } else {

            holder = (PopinputAdapter.ViewHolder) convertViewtms.getTag();
            holder.mHandlContentData = mData.get(position);


            holder.frament_handl_menu_textview   = convertViewtms.findViewById(R.id.frament_handl_menu_textview);
            holder.frament_handl_input_edittext   = convertViewtms.findViewById(R.id.frament_handl_input_edittext);
            holder.frament_handl_index_textview   = convertViewtms.findViewById(R.id.frament_handl_index_textview);


            holder.frament_handl_menu_textview.setText( holder.mHandlContentData.getMenuName());
            holder.frament_handl_input_edittext.setText(  holder.mHandlContentData.getMenuOutData() );
            holder.frament_handl_index_textview.setText(  holder.mHandlContentData.getMenuid()+"");

            holder.frament_handl_input_edittext.setTag(holder);

        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(PopInputData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, PopInputData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(PopInputData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {


        public TextView frament_handl_index_textview   = null;
        public TextView frament_handl_menu_textview   = null;
        public EditText frament_handl_input_edittext   = null;


        public PopInputData mHandlContentData = null;


    }


}
